#include "mainwindow.h"
#include "ui_mainwindow.h"
#include "dialog.h"
#include "sqlitedboperator.h"
#include "curtain_control.h"
#include "all_drive.h"
#include "app_player.h"
#include "QPainter"
#include "clockmenu.h"
all_drive a_drive;

int curtain_arr[5]={100,0,0,0,0};
int light_arr[5]={0,0,0,0,0};//初始灯光控制

SqliteDBAOperator mydb;
int kbdfd,ledsfd,beepfd,adcfd;
MainWindow::MainWindow(QWidget *parent) :
    QMainWindow(parent),
    ui(new Ui::MainWindow)
{
    ui->setupUi(this);
    startinit();
    //a_drive.startinit();
    qDebug()<<a_drive.Messages;
    qDebug()<<"kbdfd="<<kbdfd;
    qDebug()<<"ledsfd="<<ledsfd;
    qDebug()<<"beepfd="<<beepfd;
    qDebug()<<"adcfd="<<adcfd;

}

MainWindow::~MainWindow()
{
    delete ui;
}

void MainWindow::paintEvent(QPaintEvent *)
{
    QPainter p;
    p.begin(this);
    p.drawPixmap(0,0,width(),height(),QPixmap(":/images/Lonely Night .png"));
}


void MainWindow::startinit()
{

    //QPixmap mypixmap(":/images/jiaju.jpeg");
    //ui->label->setPixmap(mypixmap);
    //ui->label->show();
/*
    mydb.OpenDb();
    QString tablename="EVENTTABLE";
    if(!mydb.IsTaBexists(tablename))
        mydb.createTable();
*/
    realtimeclock.setInterval(1000);
    QTime currenttime = QTime::currentTime();
    QString currentTimeString = currenttime.toString("hh:mm:ss");
    ui->timelabel->setText(currentTimeString);
    QDate currentDate = QDate::currentDate();
    QString currentDateString = currentDate.toString("yyyy-MM-dd");
    ui->datelabel->setText(currentDateString);
    QDateTime currentWeek= QDateTime::currentDateTime();
    QString currentWeekString = currentWeek.toString("dddd");
    ui->weeklabel->setText(currentWeekString);

    connect(&realtimeclock,&QTimer::timeout,[&](){
        QTime currenttime = QTime::currentTime();
        QString currentTimeString = currenttime.toString("hh:mm:ss");
        ui->timelabel->setText(currentTimeString);
        QDate currentDate = QDate::currentDate();
        QString currentDateString = currentDate.toString("yyyy-MM-dd");
        ui->datelabel->setText(currentDateString);
        QDateTime currentWeek= QDateTime::currentDateTime();
        QString currentWeekString = currentWeek.toString("dddd");
        ui->weeklabel->setText(currentWeekString);
    });
    realtimeclock.start();

    ThreadModBus= new ModBusService(this);

    connect(ThreadModBus,&QThread::finished
                ,ThreadModBus,&QObject::deleteLater);//线程结束后调用deleteLater来销毁分配的内存
    sleep(1);
    ThreadModBus->start();

}

void MainWindow::on_pushButton_3_clicked()
{
    Dialog *d=new Dialog(this);
    d->setGeometry(this->geometry());
    d->show();
    this->hide();
}

void MainWindow::on_pushButton_2_clicked()
{
    Curtain_Control *C=new Curtain_Control(this);
    C->setGeometry(this->geometry());
    C->show();
    this->hide();
}

void MainWindow::on_pushButton_clicked()
{
    app_player *player=new app_player(this);
    player->setGeometry(this->geometry());
    player->show();
    this->hide();
}

void MainWindow::on_pushButton_5_clicked()
{
	lock->show();
    this->close();
}

void MainWindow::on_pushButton_4_clicked()
{

}

/*
    app_player *player=new app_player(this);
    player->show();
    this->hide();
*/
